p1<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.name"
          placeholder="品牌名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="addBrand()"
          >品牌添加</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="delBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="brandList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="name" label="品牌名称" width="180">
      </el-table-column>

      <el-table-column prop="firstLetter" label="品牌首字母" width="180">
      </el-table-column>

      <el-table-column prop="logo" label="品牌logo">
        <template slot-scope="scope">
          <div><img :src="scope.row.logo" /></div>
        </template>
      </el-table-column>

      <el-table-column prop="showStatus" label="是否展示">
        <template slot-scope="scope">
          <div>{{ scope.row.showStatus == 0 ? "展示" : "不展示" }}</div>
        </template>
      </el-table-column>

      <el-table-column prop="productCount" label="商品数"> </el-table-column>

      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="
                handleEdit(scope.$index, scope.row);
              "
              >修改</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchMap.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </div>
    <!-- 添加品牌 -->

    <el-dialog
      :title="brand.id ? '品牌修改' : '品牌添加'"
      :visible.sync="dialogFormVisible_add"
      :close-on-click-modal="false"
    >
      <el-form :model="brand">
        <el-form-item label="品牌名称" >
          <el-input v-model="brand.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌首字母" >
          <el-input v-model="brand.firstLetter" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌logo">
          <el-upload
            :headers=this.headers
            ref='upload'
            action="http://localhost:8080/upload"
            list-type="picture-card"
            name="headerImage"
            :on-success="uploadSuccess"
          >
            <img :src="this.brand.logo" />
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDate()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改品牌 -->
    <!-- <el-dialog title="品牌修改" :visible.sync="dialogFormVisible_edit">
      <el-form>
        <el-form-item label="品牌名称" :label-width="formLabelWidth">
          <el-input v-model="editDialogData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌首字母" :label-width="formLabelWidth">
          <el-input
            v-model="editDialogData.firstLetter"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否展示" :label-width="formLabelWidth">
          <el-input
            v-model="editDialogData.showStatus"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editConfirm()">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>
<script>
import {search, save, del, edit, delAll } from "@/api/brand";
export default {
  data() {
    return {
      dialogFormVisible_edit: false, //编辑弹窗开关
      dialogFormVisible_add: false, //添加弹窗开关
      brandList: [],
      total: 0,
      headers:{},
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      delData: {}, //删除的信息
      delAllData: {}, //批量删除数据
      editDialogData: {}, //编辑弹框显示的数据
      brand: {}, //品牌信息
    };
  },
  methods: {
    addBrand(){
       this.dialogFormVisible_add = true;
       this.brand={};
       this.headers={'token':sessionStorage.getItem('token')}
    },
    //搜索
    search() {
      var _this = this;
      // if(this.searchMap.data.name=""){
      //   this.searchMap.data={};
      // }
      var searchMap1= {
        page: 1,
        limit: 5,
        data: {},
      };
      if (this.searchMap.data.name==""){
        search(searchMap1).then((res) => {
        _this.brandList = res.data;
        _this.total = res.total;
      });
      }else{
         search(this.searchMap).then((res) => {
        _this.brandList = res.data;
        _this.total = res.total;
      });
      }
      
    },
    //保存
    saveDate() {
      save(this.brand).then((res) => {
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible_add = false;
      });
    },
    //删除
    handleDelete(index, row) {
      this.delData = row;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del(this.delData).then((res) => {
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        }
    );
      })
    },
    //批量删除
    handleSelectionChange(val) {
      this.delAllData = val;
      console.log(this.delAllData);
    },
    delBatch() {
      var ids = [];
      this.delAllData.forEach((item) => {
        ids.push(item.id);
      });
      console.log(ids);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delAll(ids).then((res) => {
          console.log(this.ids);
          if (res.success) {
            this.search();
            this.$message({
              showClose: true,
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },

    //编辑
    handleEdit(index, row) {

      let that = this;
      that.dialogFormVisible_add = true;
      that.brand = row;

     // el-upload-list__item-thumbnail
     this.$nextTick(()=>{
               this.$refs.Upload.clearFiles();//清空图片
             });
      console.log(that.brand);
    },

    editConfirm() {
      edit(this.brand).then((res) => {
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible_edit = false;
      });
    },
    uploadSuccess(response, file, fileList) {
      this.brand.logo = response.data;
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.search();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.search();
    },
  },
  created() {
    let that = this;
    that.search();
  },
};

</script>